/**
 * 
 */
package com.runningfox.humor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.runningfox.model.Joke;
import com.runningfox.model.Setting;

/**
 * @author lhy
 * 
 */
public class JokeNodeView extends LinearLayout {

	static int[] color = { 
			Color.rgb(0x85, 0x4f, 0x61),// 0x854f61,//purple
			Color.rgb(0xec, 0x70, 0x00),// 0xec7000,//orange
			Color.rgb(0xcc, 0x00, 0x00),// 0xcc0000,//red
			Color.rgb(0xb3, 0x6d, 0x00),// 0xb36d00,//brown
			Color.rgb(0x20, 0x6c, 0xff),// 0x206cff,//light blue
			Color.rgb(0x00, 0x66, 0x33),// 0x006633,//green
			Color.rgb(0x63, 0x63, 0x30) // 0x636330//green2
	};
	static int selectedBg = Color.rgb(0xd3, 0xe8, 0x61);// d3e861
	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	public final static String today = dateFormat.format(new Date());
	TextView txtTitle, txtBody, txtLike,txtComment, txtShare,txtDate,txtTag1, txtTag2, txtTag3, txtTag4,
			txtTag5;

	// Button btnMore ,btnLike,btnDislike,btnOld,btnShare;
	// LinearLayout tagBg1,tagBg2,tagBg3,tagBg4,tagBg5;
	Joke node;
	Context c;
	Humor callback;
	LayoutInflater inf;
	View view = null;
	// public boolean selected = false;
	LinearLayout tagLayer, left1, right1, left2, right2, left3,
			right3, left4, right4, left5, right5;
	ImageView imgNew;

	public JokeNodeView(Humor humor, Joke node) {
		super(humor.c);
		// TODO Auto-generated constructor stub
		this.callback = humor;
		c = callback.c;

		this.setOrientation(VERTICAL);
		inf = LayoutInflater.from(c);
		view = inf.inflate(R.layout.jokenode, null);
		this.addView(view, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		this.imgNew = (ImageView) view.findViewById(R.id.ImageNew);
		this.txtTitle = (TextView) view.findViewById(R.id.TextViewTitle);
		this.txtLike = (TextView) view.findViewById(R.id.TextLike);
		this.txtComment = (TextView) view.findViewById(R.id.TextComment);
		this.txtShare=(TextView)view.findViewById(R.id.TextShare);
		this.txtDate =(TextView) view.findViewById(R.id.TextDate);
		this.txtBody = (TextView) view.findViewById(R.id.TextViewBody);
		this.txtTag1 = (TextView) view.findViewById(R.id.TextViewTag1);
		this.txtTag2 = (TextView) view.findViewById(R.id.TextViewTag2);
		this.txtTag3 = (TextView) view.findViewById(R.id.TextViewTag3);
		this.txtTag4 = (TextView) view.findViewById(R.id.TextViewTag4);
		this.txtTag5 = (TextView) view.findViewById(R.id.TextViewTag5);
		this.left1 = (LinearLayout) view.findViewById(R.id.left1);
		this.right1 = (LinearLayout) view.findViewById(R.id.right1);
		this.left2 = (LinearLayout) view.findViewById(R.id.left2);
		this.right2 = (LinearLayout) view.findViewById(R.id.right2);
		this.left3 = (LinearLayout) view.findViewById(R.id.left3);
		this.right3 = (LinearLayout) view.findViewById(R.id.right3);
		this.left4 = (LinearLayout) view.findViewById(R.id.left4);
		this.right4 = (LinearLayout) view.findViewById(R.id.right4);
		this.left5 = (LinearLayout) view.findViewById(R.id.left5);
		this.right5 = (LinearLayout) view.findViewById(R.id.right5);
		this.tagLayer = (LinearLayout) view.findViewById(R.id.LinearLayoutTag);
//		this.deleteLayer = (LinearLayout) view.findViewById(R.id.deleteLayout);
//		this.deleteLayer.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				callback.actionDeleteFavouriteJoke(JokeNodeView.this.node);
//			}
//
//		});
		
		this.txtLike.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (ListLayerCtrl.curLayer == ListLayerCtrl.LAYER_FAVOURITE) {
					callback.actionDeleteFavouriteJoke(JokeNodeView.this.node);
				} else {
//					this.deleteLayer.setVisibility(View.GONE);
					callback.actionLike(JokeNodeView.this.node);
				}
			}
			
		});
		
		this.txtComment.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(callback.curLayer==callback.LAYER_LIST){
					callback.actionLoadJoke(JokeNodeView.this.node);
				}else if(callback.curLayer==callback.LAYER_DETAIL){
					callback.actionActiveCommentLayer();
				}
			}
			
		});
		
		this.txtShare.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				callback.actionShareJoke(JokeNodeView.this.node);
			}
			
		});
		
//		this.txtBody.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				callback.actionLoadJoke(JokeNodeView.this.node);
//			}
//			
//		});
		this.txtLike.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				TextView cur =JokeNodeView.this.txtLike; 
				switch(action){
					case MotionEvent.ACTION_DOWN:{
						cur.setBackgroundColor(Color.BLUE);
						break;
					}
					case MotionEvent.ACTION_UP:{
						cur.setBackgroundColor(0);
						break;
					}default:{
						cur.setBackgroundColor(0);
					}
				}
				return false;
			}});
		
		this.txtComment.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				TextView cur =JokeNodeView.this.txtComment; 
				switch(action){
					case MotionEvent.ACTION_DOWN:{
						cur.setBackgroundColor(Color.BLUE);
						break;
					}
					case MotionEvent.ACTION_UP:{
						cur.setBackgroundColor(0);
						break;
					}default:{
						cur.setBackgroundColor(0);
					}
				}
				return false;
			}});
		
		this.txtShare.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				TextView cur =JokeNodeView.this.txtShare; 
				switch(action){
					case MotionEvent.ACTION_DOWN:{
						cur.setBackgroundColor(Color.BLUE);
						break;
					}
					case MotionEvent.ACTION_UP:{
						cur.setBackgroundColor(0);
						break;
					}default:{
						cur.setBackgroundColor(0);
					}
				}
				return false;
			}});
		
//		this.txtBody.setOnTouchListener(new OnTouchListener(){
//
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				int action = event.getAction();
//				TextView cur =JokeNodeView.this.txtBody; 
//				switch(action){
//					case MotionEvent.ACTION_DOWN:{
//						cur.setBackgroundColor(Color.BLUE);
//						break;
//					}
//					case MotionEvent.ACTION_UP:{
//						cur.setBackgroundColor(0);
//						break;
//					}
//					default:{
//						cur.setBackgroundColor(0);
//					}
//				}
//				return false;
//			}});
		this.setNode(node);
	}

	// yyyy-MM-dd hh:MM:ss
	boolean isToday(Joke node) {
		try {
			if (node.dateStr != null) {
				if (node.dateStr.contains(today)) {
					return true;
				} else {
					return false;
				}
			} else if (node.datetime != null) {
				String datestr = this.dateFormat.format(node.datetime);
				if (datestr.contains(today)) {
					return true;
				} else {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	static String getMagicNum(int num){
		String rtn;
		if(num>999){
			rtn="1000+";
		}else if(num>99){
			rtn="100+";
		}else if(num>49){
			rtn="50+";
		}else if(num>19){
			rtn="20+";
		}else if(num>9){
			rtn="10+";
		}else {
			rtn = String.valueOf(num);
		}
		return rtn;
	}
	//969696
	static int bodycolor=Color.rgb(0x96, 0x96, 0x96);
	public void setNode(Joke node) {
		this.node = node;
//		this.txtBody.setText("("+node.id+")"+node.body);
		this.txtBody.setText(node.body);
		this.txtTitle.setText(node.title);
		if(node.dateStr!=null){
			this.txtDate.setText(node.dateStr.substring(0, 10));
		}
		if(callback.curLayer==callback.LAYER_DETAIL){
			this.txtBody.setMaxLines(1000);
		}else{
			this.txtBody.setMaxLines(10);
		}
		
		if(Setting.theme){
			this.txtBody.setTextColor(color_unread);
		}
		else{
			this.txtBody.setTextColor(bodycolor);
		}
//		this.buildTag();
		if (node.read == 0) {
			if(Setting.theme){
				this.txtTitle.setTextColor(Color.WHITE);
			}
			else{
				this.txtTitle.setTextColor(color_unread);
			}
		}else{
//			this.txtBody.setTextColor(Color.WHITE);
		}
		if (isToday(node)&&node.read==0) {
			this.imgNew.setVisibility(View.VISIBLE);
		} else {
			this.imgNew.setVisibility(View.GONE);
		}
		
		
		
		if(node.comment==null||node.comment.equals("0")){
//			this.txtComment.setVisibility(View.GONE);
			this.txtComment.setText(callback.getString(R.string.comment));
		}else{
			try{
				String commentStr = node.comment;
				int commentInt = Integer.valueOf(node.comment);
				commentStr=getMagicNum(commentInt);
				this.txtComment.setVisibility(View.VISIBLE);
				this.txtComment.setText(callback.getString(R.string.comment)+"("+commentStr + ")");
			}catch(Exception e){
				e.printStackTrace();
			}		
		}
		
		if (node.like == null || node.like.equals("0")) {
//			this.txtLike.setVisibility(View.GONE);
			this.txtLike.setText(callback.getString(R.string.like));
		} else {
			try{
				String likeStr = node.like;
				int likeInt = Integer.valueOf(node.like);
				likeStr=getMagicNum(likeInt);
				this.txtLike.setVisibility(View.VISIBLE);
				this.txtLike.setText(callback.getString(R.string.like)+"("+likeStr +")" );
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		if (ListLayerCtrl.curLayer == ListLayerCtrl.LAYER_FAVOURITE) {
			this.txtLike.setText(callback.getString(R.string.unlike));
		}
		this.buildTag();
	}

	class TagTouchListner implements OnTouchListener {
		TextView t ;
		String txt;
		public TagTouchListner(TextView t,String txt) {
			super();
			this.t = t;
			this.txt=txt;
		}

		@Override
		public boolean onTouch(View view, MotionEvent event) {
			// TODO Auto-generated method stub
			int action = event.getAction();
			switch (action) {
			case MotionEvent.ACTION_UP: {
				t.setTextColor(Color.WHITE);
				return false;
			}
			case MotionEvent.ACTION_DOWN: {
				t.setTextColor(Color.TRANSPARENT);
				callback.actionSendComment(node, txt);
				break;
			}default:{
				t.setTextColor(0);
			}
			}
			return true;
		}

		
	}
	// boolean TagBuilded = false;

	void buildTag() {
		// if (TagBuilded) {
		// return;
		// }
		try {
			this.txtTag1.setVisibility(View.GONE);
			this.txtTag2.setVisibility(View.GONE);
			this.txtTag3.setVisibility(View.GONE);
			this.txtTag4.setVisibility(View.GONE);
			this.txtTag5.setVisibility(View.GONE);
			this.left1.setVisibility(View.GONE);
			this.left2.setVisibility(View.GONE);
			this.left3.setVisibility(View.GONE);
			this.left4.setVisibility(View.GONE);
			this.left5.setVisibility(View.GONE);
			this.right1.setVisibility(View.GONE);
			this.right2.setVisibility(View.GONE);
			this.right3.setVisibility(View.GONE);
			this.right4.setVisibility(View.GONE);
			this.right5.setVisibility(View.GONE);
			
			String tag = node.tag;
			Log.v("test", tag);
			StringTokenizer tokens = new StringTokenizer(tag, ";");
			int index = 1;
			while (tokens.hasMoreTokens() && index <= 5) {
				Log.v("loop",  this.getClass().getName()+" buildtag");
				String str = tokens.nextToken();
				int mid = str.indexOf(':');
				int colorIndex = Integer.valueOf(str.substring(0, mid));
				String txt = str.substring(mid + 1);
				switch (index) {
				case 1: {
					this.txtTag1.setBackgroundColor(color[colorIndex]);
					this.txtTag1.setText(txt);
//					this.txtTag1.setOnClickListener(new OnClickListener(){
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//							callback.actionSendComment(node, JokeNodeView.this.txtTag1.getEditableText().toString());
//						}
//						
//					});
					if(this.callback.curLayer==this.callback.LAYER_DETAIL){
						this.txtTag1.setOnTouchListener(new TagTouchListner(this.txtTag1,txt));
					}
					this.left1.setBackgroundColor(color[colorIndex]);
					this.right1.setBackgroundColor(color[colorIndex]);
					this.txtTag1.setVisibility(View.VISIBLE);
					this.left1.setVisibility(View.VISIBLE);
					this.right1.setVisibility(View.VISIBLE);
					break;
				}
				case 2: {
					this.txtTag2.setBackgroundColor(color[colorIndex]);
					this.txtTag2.setText(txt);
//					this.txtTag2.setOnClickListener(new OnClickListener(){
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//							callback.actionSendComment(node, JokeNodeView.this.txtTag2.getEditableText().toString());
//						}
//						
//					});
					if(this.callback.curLayer==this.callback.LAYER_DETAIL){
						this.txtTag2.setOnTouchListener(new TagTouchListner(this.txtTag2,txt));
					}
					this.left2.setBackgroundColor(color[colorIndex]);
					this.right2.setBackgroundColor(color[colorIndex]);
					this.txtTag2.setVisibility(View.VISIBLE);
					this.left2.setVisibility(View.VISIBLE);
					this.right2.setVisibility(View.VISIBLE);
					break;
				}
				case 3: {
					this.txtTag3.setBackgroundColor(color[colorIndex]);
					this.txtTag3.setText(txt);
//					this.txtTag3.setOnClickListener(new OnClickListener(){
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//							callback.actionSendComment(node, JokeNodeView.this.txtTag3.getEditableText().toString());
//						}
//						
//					});
					if(this.callback.curLayer==this.callback.LAYER_DETAIL){
						this.txtTag3.setOnTouchListener(new TagTouchListner(this.txtTag3,txt));
					}
					this.left3.setBackgroundColor(color[colorIndex]);
					this.right3.setBackgroundColor(color[colorIndex]);
					this.txtTag3.setVisibility(View.VISIBLE);
					this.left3.setVisibility(View.VISIBLE);
					this.right3.setVisibility(View.VISIBLE);
					break;
				}
				case 4: {
					this.txtTag4.setBackgroundColor(color[colorIndex]);
					this.txtTag4.setText(txt);
//					this.txtTag4.setOnClickListener(new OnClickListener(){
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//							callback.actionSendComment(node, JokeNodeView.this.txtTag4.getEditableText().toString());
//						}
//						
//					});
					if(this.callback.curLayer==this.callback.LAYER_DETAIL){
						this.txtTag4.setOnTouchListener(new TagTouchListner(this.txtTag4,txt));
					}
					this.left4.setBackgroundColor(color[colorIndex]);
					this.right4.setBackgroundColor(color[colorIndex]);
					this.txtTag4.setVisibility(View.VISIBLE);
					this.left4.setVisibility(View.VISIBLE);
					this.right4.setVisibility(View.VISIBLE);
					break;
				}
				case 5: {
					this.txtTag5.setBackgroundColor(color[colorIndex]);
					this.txtTag5.setText(txt);
//					this.txtTag5.setOnClickListener(new OnClickListener(){
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//							callback.actionSendComment(node, JokeNodeView.this.txtTag5.getEditableText().toString());
//						}
//						
//					});
					if(this.callback.curLayer==this.callback.LAYER_DETAIL){
						this.txtTag5.setOnTouchListener(new TagTouchListner(this.txtTag5,txt));
					}
					this.left5.setBackgroundColor(color[colorIndex]);
					this.right5.setBackgroundColor(color[colorIndex]);
					this.txtTag5.setVisibility(View.VISIBLE);
					this.left5.setVisibility(View.VISIBLE);
					this.right5.setVisibility(View.VISIBLE);
					break;
				}
				}
				index++;
			}
			this.tagLayer.setVisibility(View.VISIBLE);
			// this.TagBuilded = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	//B7C334
	static int color_unread = Color.rgb(0xB7, 0xC3, 0x34);
//	public void setExpended(boolean b) {
//		if (b) {
//			this.tagLayer.setVisibility(View.VISIBLE);
//			// view.setBackgroundColor(selectedBg);
//			this.txtBody.setMaxLines(10);
//			this.txtBody.setTextColor(color_unread);
//			this.buildTag();
//			this.deleteLayer.setVisibility(View.GONE);
//
//		} else {
//			this.tagLayer.setVisibility(View.GONE);
//			if (ListLayerCtrl.curLayer == ListLayerCtrl.LAYER_FAVOURITE) {
//				this.deleteLayer.setVisibility(View.VISIBLE);
//			} else {
//				this.deleteLayer.setVisibility(View.GONE);
//			}
//			this.txtBody.setMaxLines(1);
//			// #5a5a5a
//			this.txtBody.setTextColor(Color.rgb(0x5a, 0x5a, 0x5a));
//			this.txtTitle.setTextColor(Color.WHITE);
//		}
//	}

	
	public void setOnline(boolean b){
		if(b){
			this.txtLike.setVisibility(View.VISIBLE);
			this.txtComment.setVisibility(View.VISIBLE);
		}else{
			this.txtLike.setVisibility(View.GONE);
			this.txtComment.setVisibility(View.GONE);
		}
	}
}